import Taro from "@tarojs/taro";
import { logError, pageToLogin, tryRefreshToken } from "../utils";
import getBaseUrl from "./baseUrl";
import { HTTP_STATUS } from "./status";

// const token = Taro.getStorageSync('access_token') || '';
let isSucc = false;
export default {
  async baseOptions(params, method = "GET", isWallet = false) {
    console.log('params', params);
    let { url, data } = params;
    // let token = getApp().globalData.token
    // if (!token) login()
    const contentType = params.contentType || "application/json";
    // const base = !isWallet ? getBaseUrl() : 'http://139.9.231.74:8899';
    const base = !isWallet ? getBaseUrl() : 'https://zc.nyjinguan.com';
    // const base = !isWallet ? getBaseUrl() : 'http://192.168.3.26:8899';
    const accessToken = tryRefreshToken();
    console.log('accessToken', accessToken);
    const header = accessToken
      ? {
          "content-type": contentType,
          // "appid": 'wx2aa935afd89c3b84',
          "appid": 'wx52f6aba688ce70f6',
          Authorization: "Bearer " + accessToken,
        }
      : {
          "content-type": contentType,
          "appid": 'wx52f6aba688ce70f6',
        };
    console.log('DATA', data);
    const option = {
      isShowLoading: false,
      loadingText: "正在加载",
      url: base + url,
      data: data,
      method: method,
      header,
      // complete(res) {
      //   console.log('(res)', res);
      // },
      success(res) {
        // console.log('request请求成功', res);
        isSucc = true
        if (res.statusCode === HTTP_STATUS.NOT_FOUND) {
          return logError("api", "请求资源不存在");
        } else if (res.statusCode === HTTP_STATUS.BAD_GATEWAY) {
          return logError("api", "服务端出现了问题");
        } else if (res.statusCode === HTTP_STATUS.FORBIDDEN) {
          Taro.setStorageSync("Authorization", "");
          pageToLogin();
          return logError("api", "没有权限访问");
        } else if (res.statusCode === HTTP_STATUS.SUCCESS) {
          // if (method === 'POST'){
          //   return res
          // }
          return res.data ? res.data : res;
        }
      },
      fail:(res)=>{
        // console.log('request请求失败', res);
        isSucc = false;
        setTimeout(() => {
          // console.log('isSucc', isSucc);
          // if(!isSucc){
            // wx.request().abort() // 手动中断请求
            const pages = Taro.getCurrentPages();
            console.log('pages', pages);
            wx.showModal({
              title: '检测到您设备刚才有网络波动情况,请重新进入小程序.',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  // Taro.reLaunch({
                  //   url: '/pages/index/index'
                  // })
                  const app = Taro.getApp();
                  console.log('APP', app);
                  // 退出小程序
                  Taro.exitMiniProgram({
                    success: (res) => {
                      console.log('退出的回调', res);
                      // 重新跳转至小程序
                      // Taro.navigateToMiniProgram({
                      //   appId: 'wx52f6aba688ce70f6',
                      //   path: 'pages/index/index'
                      // })
                    }
                  })
                  // 加载首页
                  // Taro.reLaunch({
                  //   url: '/pages/index/index'
                  // })
                }}
            })
          // }
        }, 100);

      },
      error(e) {
        logError("api", "请求接口出现问题", e);
        console.log("api", "请求接口出现问题", e)
      },
    };
    // if (url.indexOf('https//apis') !== -1) option.url = url;
    return Taro.request(option);
  },
  get(url, data = "", isWallet) {
    let option = { url, data };
    return this.baseOptions(option, "GET", isWallet);
  },
  post: function (url, data, isWallet) {
    let option = { url, data };
    return this.baseOptions(option, "POST", isWallet);
  },
  postFile: function (url, data, isWallet) {
      let option = { url, data, contentType: 'multipart/form-data',};
      return this.baseOptions(option, "POST", isWallet);
  },
  put(url, data = "", isWallet) {
    let option = { url, data };
    return this.baseOptions(option, "PUT", isWallet);
  },

  delete(url, data = "", isWallet) {
    let option = { url, data };
    return this.baseOptions(option, "DELETE", isWallet);
  },
};
